package controllers

import (
	"github.com/revel/revel"
	"dataPublisher/app/models"
)

type Data struct {
	Controller
}

func (c Data) GetData() revel.Result {
	type Req struct {
		ID int64 	`json:"id" valid:"Required"`
		Begin int64 `json:"begin" valid:"Required"`
		End int64   `json:"end" valid:"Required"`
		Step string `json:"step" valid:"Required"`
	}
	var req Req
	// parse
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	data := []models.PointData{}
	if err := models.GetData(req.ID, req.Begin, req.End, req.Step, &data); err != nil {
		return c.RenderResult(err)
	}

	return c.RenderResult(data)
}

func (c Data) GetTrouble() revel.Result {
	type Req struct {
		Begin int64 `json:"begin" valid:"Required"`
		End int64   `json:"end" valid:"Required"`
		Count int `json:"count" valid:"Required"`
	}
	var req Req
	// parse
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	data := []models.DataAlert{}
	if err := models.GetTroubleData(req.Begin, req.End, req.Count, &data); err != nil {
		return c.RenderResult(err)
	}

	return c.RenderResult(data)
}


